package com.school.food.controller;


import com.github.pagehelper.PageInfo;
import com.school.food.domain.Food;
import com.school.food.domain.FoodImg;
import com.school.food.model.FoodModel;
import com.school.food.service.FoodImgService;
import com.school.food.service.FoodService;
import com.school.food.service.OrderItemService;
import com.school.support.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.school.support.base.Page;
import com.school.support.example.Example;
import com.school.support.example.ExampleType;

import com.school.support.response.Response;
import com.school.support.StringUtil;

import java.util.List;

/**
 * Created by 执笔 on 2017/3/5.
 */
@RequestMapping("food")
@Controller
public class FoodController extends BaseController {

    @Autowired
    private FoodService foodService;
    @Autowired
    private FoodImgService foodImgService;
    @Autowired
    private OrderItemService orderItemService;

    /**
     * 详情页
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Integer userId = sessionUser() == null ? 0 : sessionUser().getId();
        FoodModel foodModel = foodService.selectModelByPk(id,userId);
        List<FoodImg> foodImgs = foodImgService.selectByFood(id);
        setModelAttribute(model, foodModel, foodImgs);
        return "food/detail";
    }

    @RequestMapping("list")
    public String list(Food food, Model model,Page page,String collect) {
        Example example = Example.getInstance()
                .addOrder("f.sort", ExampleType.OrderType.DESC)
                .addParam("f.category", food.getCategory())
                .addParam("f.name", food.getName(), ExampleType.Operation.LIKE)
                .addParam("f.status", "1");
        if(!StringUtil.isEmpty(collect)){
            example.addParam("c.id  is not null");
        }
        Integer userId = sessionUser() == null ? 0 : sessionUser().getId();
        PageInfo<FoodModel> pageInfo = foodService.selectModel(example,userId, page);
        setModelAttribute(model, pageInfo);
        return "food/list";
    }

    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id){
        orderItemService.deleteByPK(id);
        return refresh();
    }

}
